using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyKTV
{
    class DBHelper
    {
        public static string connStr = "server=.;Initial Catalog=KTV;Integrated Security=true";
        public static SqlConnection conn;


        /// <summary>
        /// 1.增删改的方法
        /// </summary>
        /// <param name="sqlStr">SQL语句</param>
        /// <returns>影响行数</returns>
        public static int getNonQuery(string sqlStr)
        {
            int result = -1;
            try
            {
                conn = new SqlConnection(connStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                conn.Close();
            }

            return result;
        }


        /// <summary>
        /// 2.获取第一行第一列的值
        /// </summary>
        /// <param name="sqlStr">SQL语句</param>
        /// <returns>第一行第一列的结果</returns>
        public static object getScalar(string sqlStr)
        {
            object result = null;
            try
            {
                conn = new SqlConnection(connStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                //获取第一行第一列的值
                result = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }


        /// <summary>
        /// 3.查询多行数据
        /// </summary>
        /// <param name="sqlStr">SQL语句</param>
        /// <returns>当前行的数据</returns>
        public static SqlDataReader getReader(string sqlStr)
        {
            SqlDataReader result = null;
            try
            {
                conn = new SqlConnection(connStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                //获取DataReader
                result = cmd.ExecuteReader();
                //注意！！！这里千万不能关闭通道
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 一次查询多行数据
        /// </summary>
        /// <param name="sqlStr">SQL语句</param>
        /// <returns>查询多行数据</returns>
        public static DataTable getDataTable(string sqlStr)
        {
            DataTable result = null;
            try
            {
                conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);          
                DataSet ds = new DataSet();
                da.Fill(ds);            
                result = ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
    }
}